﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Communergy.Common;
using System.Data.SqlClient;
using System.Configuration;
using Communergy.Repository.EntityFramework;

namespace Communergy.Repository
{
    public class EntityRealtimeRepository: IRealtimeRepository
    {
        communergyEntities db;

        public EntityRealtimeRepository(IUnitOfWork unitOfWork)
        {
            //will return null if not communergyEntities
            db = unitOfWork as communergyEntities;
            if (db == null)
                throw new ArgumentOutOfRangeException("unitOfWork");        
        }
       

        #region IAutomaticRepository Members

        public void Insert_ElectricityRealtime(string meterId, List<RealtimeEnergyReading> realtimeEnergyReadings)
        {
            //check meter exists
            var meterIdGuid = new Guid(meterId);
           // using (var db = new communergyEntities(connString))
            {
                var thisMeter = db.ElectricityMeter.Where(m => m.me_meterGuid.Value == meterIdGuid).FirstOrDefault();
                if (thisMeter != null)
                {
                    foreach (var realtimeEnergyReading in realtimeEnergyReadings)
                    {
                        //using (var dbSave = new communergyEntities(connString))
                        {
                            thisMeter.ElectricityRealtime.Add(new ElectricityRealtime
                            {
                                cc_dateTime = realtimeEnergyReading.ReadingDateTime,
                                cc_watts = realtimeEnergyReading.Watts,
                                cc_joules = realtimeEnergyReading.Joules
                            });
                        }
                    }
                }
            }
        }

  
        #endregion

        #region IRealtimeRepository Members


        public List<RealtimeEnergyReading> Get_ElectricityRealtime(string meterId, DateTime dateFrom, DateTime dateTo)
        {
            throw new NotImplementedException();
        }

        public RealtimeEnergyReading GetLastElectricityRealtime(string meterId)
        {
            try
            {
                //check meter exists
                var meterIdGuid = new Guid(meterId);
                //using (var db = new communergyEntities(connString))
                {
                    var meter = db.ElectricityMeter.Where(m => m.me_meterGuid.Value == meterIdGuid).FirstOrDefault();
                    if (meter != null)
                    {
                        var lastReading = (
                                            from re in db.ElectricityRealtime
                                            where re.cc_dateTime < DateTime.Now && re.ElectricityMeter.me_meterGuid == meterIdGuid
                                            orderby re.cc_dateTime descending
                                            select new RealtimeEnergyReading
                                            {
                                                ReadingDateTime = re.cc_dateTime,
                                                Joules = (int)re.cc_joules.Value,
                                                Watts = (int)re.cc_watts
                                            }
                                        ).FirstOrDefault();
                        return lastReading;
                    }
                    else
                    {
                        return null;
                    }
                }
            }
            catch (Exception e)
            {

                return null;
            }
        }

        #endregion

        #region IRealtimeRepository Members


        public HistoricalEnergyReading GetCumulativeElectricityRealtime(string meterId, DateTime dateFrom, DateTime dateTo)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
